package com.wu.leetcode.leetcode228;

import java.util.ArrayList;
import java.util.List;

/**
 * Leetcode228-汇总区间
 * @author lynn
 * @date 2020/10/29 - 11:08
 */
public class Solution {
    public static List<String> summaryRanges(int[] nums) {
        if (nums == null || nums.length == 0) {
            return new ArrayList<>();
        }
 /*       List<String> list = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            int start = nums[i];
            int temp = start;
            int end = 0;
            int index = i + 1;
            while (index < nums.length && nums[index] - temp == 1) {
                temp = nums[index];
                index++;
            }
            end = nums[index - 1];
            i += end - start;
            if (end != start) {
                list.add(new StringBuilder().append(start).append("->").append(end).toString());
            } else {
                list.add(String.valueOf(start));
            }
        }
        return list;*/

        List<String> summary = new ArrayList<>();
        for (int i, j = 0; j < nums.length; ++j){
            i = j;
            // try to extend the range [nums[i], nums[j]]
            while (j + 1 < nums.length && nums[j + 1] == nums[j] + 1) {
                ++j;
            }
            // put the range into the list
            if (i == j) {
                summary.add(nums[i] + "");
            } else {
                summary.add(nums[i] + "->" + nums[j]);
            }
        }
        return summary;

    }

    public static void main(String[] args) {
        int[] ints = {-1,0,1};
        List<String> strings = summaryRanges(ints);
        System.out.println(strings);
    }
}